/**
 * meiguang8.com
 * Copyright (C) 2016-2019 All Rights Reserved.
 */
package com.yuanchuang.yp.framework.common;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import java.io.Serializable;

/**
 * admin 和api对外返回实体
 * 兼容.net 的方式
 *
 * @author yuanyuan.zhu
 * @version ApiResult.java, v 0.1 2019-04-18 16:47 yuanyuan.zhu
 */

public class CommonResult<T> implements Serializable {
    private static final long serialVersionUID = -8876152938111482090L;
    /**
     * 状态码
     */
    private Integer code;
    /**
     * 结果信息
     */
    private String message;
    /**
     * 结果数据
     */
    private T data;

    /**
     *
     */
    private Boolean result;
    /**
     * 总条目数，兼容.net
     */
    private Long count = 0L;


    public CommonResult() {
    }

    public CommonResult(CommonCode apiCode, Boolean result) {
        this(apiCode.getCode(), apiCode.getMessage(), result);
    }


    public CommonResult(Integer code, String message, Boolean result) {
        this.code = code;
        this.message = message;
        this.result = result;
    }

    public CommonResult(CommonCode apiCode, T data, Boolean result, Long count) {
        this(apiCode.getCode(), apiCode.getMessage(), result);
        this.data = data;
        this.count = count;
    }

    public CommonResult(CommonCode apiCode, T data, Boolean result) {
        this(apiCode.getCode(), apiCode.getMessage(), result);
        this.data = data;
    }


    public static <T> CommonResult ok(T value) {
        return new CommonResult(CommonCode.OK, value, Boolean.TRUE);
    }

    public static <T> CommonResult ok(T value, Long count) {
        return new CommonResult(CommonCode.OK, value, Boolean.TRUE, count);
    }



    public static CommonResult error(CommonCode code) {
        return new CommonResult(code, Boolean.FALSE);
    }

    public static <T> CommonResult error(CommonCode code, T value) {
        return new CommonResult(code, value, Boolean.FALSE);
    }

    public static CommonResult error(String message) {
        return new CommonResult(CommonCode.INTERNAL_SERVER_ERROR.getCode(), message, Boolean.FALSE);
    }


    public Integer getCode() {
        return this.code;
    }

    public CommonResult setCode(int code) {
        this.code = code;
        return this;
    }

    public String getMessage() {
        return this.message;
    }

    public CommonResult setMessage(String message) {
        this.message = message;
        return this;
    }


    public Long getCount() {
        return this.count;
    }

    public CommonResult setCount(Long count) {
        this.count = count;
        return this;
    }

    /**
     * Setter method for property <tt>result</tt>.
     *
     * @param result value to be assigned to property result
     */
    public CommonResult setResult(Boolean result) {
        this.result = result;
        return this;
    }

    /**
     * Getter method for property <tt>result</tt>.
     *
     * @return property value of result
     */
    public Boolean getResult() {
        return this.result;
    }

    public T getData() {
        return this.data;
    }

    public void setData(T data) {
        this.data = data;
    }


    @Override
    public String toString() {
        return ReflectionToStringBuilder.reflectionToString(this,
                ToStringStyle.SHORT_PREFIX_STYLE);
    }
}
